#include "head.h"

int sendFile(int net_fd){


    char *file_name = "1b.txt";

    // 打开文件
    int file_fd = open(file_name, O_RDWR);

    // 获得文件信息
    struct stat st;
    memset(&st, 0, sizeof(st));
    fstat(file_fd, &st);

    //printf("file_size: %ld \n", st.st_size);
    off_t file_size = st.st_size;
    send(net_fd, &file_size, sizeof(off_t), MSG_NOSIGNAL);


    // 写给客户端文件名字
    int name_len = strlen(file_name);
    send(net_fd, &name_len, sizeof(int), MSG_NOSIGNAL);
    send(net_fd, file_name, name_len, MSG_NOSIGNAL);


    // mmap映射磁盘文件
    void *p = mmap(NULL, st.st_size,  PROT_READ|PROT_WRITE, MAP_SHARED, file_fd, 0);

    printf("st_size: %d \n", st.st_size);
    send(net_fd, p, st.st_size, MSG_NOSIGNAL);

    munmap(p, st.st_size);

    return 0;
}

